﻿using Hopeless.Metaphysical;
using Hopeless.Physical;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hopeless.Physical
{
    class Wave
    {
        public Wave(int number, int totalZombies, int totalConcurrentZombies)
        {
            _number = number;
            _totalZombies = totalZombies;
            _totalConcurrentZombies = totalConcurrentZombies;
            _deadZombies = new List<Zombie>();
            _spawnedZombies = new List<Zombie>();

            this.InitializeZombies();
        }

        private int _number;
        public int Number
        {
            get { return _number; }
        }

        private List<Zombie> _zombies;
        internal List<Zombie> Zombies
        {
            get { return _zombies; }
        }

        private int _totalZombies;
        public int TotalZombies
        {
            get { return _totalZombies; }
        }

        private int _totalConcurrentZombies;
        public int TotalConcurrentZombies
        {
            get { return _totalConcurrentZombies; }
        }

        private List<Zombie> _spawnedZombies;
        internal List<Zombie> SpawnedZombies
        {
            get { return _spawnedZombies; }
        }

        private List<Zombie> _deadZombies;
        internal List<Zombie> DeadZombies
        {
            get { return _deadZombies; }
        }

        private void InitializeZombies()
        {
            _zombies = new List<Zombie>();
            for (int i = 0; i < _totalZombies; i++)
            {
                var zombie = new Zombie();
                zombie.Initialize();
                _zombies.Add(zombie);
            }
        }

        public void Update(GameTime gameTime)
        {
            var random = new Random();

            _deadZombies.AddRange(_spawnedZombies.Where(z => z.IsDead));
            _spawnedZombies.RemoveAll(z => z.IsDead);

            int createCount = _totalConcurrentZombies - _spawnedZombies.Count;
            for (int i = 0; i < createCount && _zombies.Any(); i++)
            {
                var zombie = _zombies.First();
                _zombies.Remove(zombie);
                _spawnedZombies.Add(zombie);
                
                var spawnIndex = random.Next(0, HopelessGame.Universe.ZombieSpawns.Count);
                var spawn = HopelessGame.Universe.ZombieSpawns[spawnIndex];
                spawn.SpawnZombie(zombie);
            }
        }
    }
}
